#!/bin/bash

function install_deb()
{
	local nvidia_apt_source="/etc/apt/sources.list.d/nvidia-l4t-apt-source.list"
	local bashrc="/home/rk/.bashrc"

	echo -e "\n>>> [INFO] Install deb: nvidia-jetpack $@"

	sed -i "s/^#deb/deb/g" ${nvidia_apt_source}
	apt update

	apt install -y nvidia-jetpack $@

	echo 'export PATH=${PATH}:/usr/local/cuda/bin' >> ${bashrc}
	echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/lib64' >> ${bashrc}
	source ${bashrc}

	sed -i "s/^deb/#deb/g" ${nvidia_apt_source}
	apt update
}

function install_mcu_app()
{
	local mcu_app=CIDI_MCU_Control
	local mcu_app_version=V1
	local mcu_app_dir=/opt/rk/mcu

	echo -e "\n>>> [INFO] Upload CIDI_MCU_Control files into /home/rk directory"
	read -p ">>> When upload ok, press any key to continue:" key
	if [[ ! -f /home/rk/${mcu_app}_${mcu_app_version} ]]; then
	    echo "CIDI_MCU_Control files does not exit."
	    exit 1
	fi
	mkdir -p ${mcu_app_dir}
	mv /home/rk/${mcu_app}* ${mcu_app_dir}
	mv ${mcu_app_dir}/${mcu_app}_${mcu_app_version} ${mcu_app_dir}/${mcu_app}
	chmod a+x ${mcu_app_dir}/${mcu_app}
}

if [[ $(id -u) -ne 0 ]]; then
    echo -e "\n[ERROR] Please run as root\n"
    exit 1
fi

install_deb
#install_mcu_app

rm $0
